package com.cti.ftpservice.localParseRunnable;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.cti.ftpservice.service.FtpFileInfoService;
import com.cti.ftpservice.service.OnixDataService;
import com.cti.ftpservice.utils.FileHelper;
import com.cti.ftpservice.utils.FtpConfigInfo;
import com.cti.ftpservice.utils.SpringContextUtil;
import com.cti.ftpservice.vo.FtpFileInfoViewVo;
import com.cti.ftpservice.vo.OnixXmlFileParseRunnableVo;
import com.cti.ftpservice.vo.OnixXmlFileReadOnixBookBaseInfoVo;

public class OnixXmlFileParseRunnable implements Runnable {
	private static Log logger = LogFactory.getLog(OnixXmlFileParseRunnable.class);
	
	private OnixXmlFileParseRunnableVo fileParseRunnableVo;
	
	private OnixDataService onixDataService;
	
	private FtpFileInfoService ftpFileInfoService;
	
	private FtpConfigInfo ftpConfigInfo;
	
	public OnixXmlFileParseRunnable(OnixXmlFileParseRunnableVo _fileParseRunnableVo){
		fileParseRunnableVo=_fileParseRunnableVo;
		onixDataService=SpringContextUtil.getBeanById("onixDataService");
		ftpConfigInfo=SpringContextUtil.getBeanById("ftpConfigInfo");
		ftpFileInfoService=SpringContextUtil.getBeanById("ftpFileInfoService");
	}
	public void run() {
		if(fileParseRunnableVo==null)
			return;
		FtpFileInfoViewVo fileInfoVo=null;
		while ((fileInfoVo=(FtpFileInfoViewVo)fileParseRunnableVo.getFirstObjectItem())!=null) {
			try {
				if(fileParseRunnableVo.getFinished()){
					 logger.info("Onix文件解析任务完成，共处理"+fileParseRunnableVo.getDealCount()+"个文件");
					 break;
				}
				logger.info("开始解析文件："+fileInfoVo.getFileName());
				OnixXmlFileReadOnixBookBaseInfoVo infoVo=null;
				infoVo=onixDataService.readOnixXmlFileCommonMethod(fileInfoVo.getId(), fileInfoVo.getLocalFtpFilePath(), fileInfoVo.getSaveRecordCount());
				if(infoVo!=null){
					if(infoVo.getBookBaseInfoVos()!=null&&infoVo.getBookBaseInfoVos().size()>0){
						fileInfoVo.setTotalRecordCount(infoVo.getTotalRecordCount());
						logger.info("文件："+fileInfoVo.getFileName()+"解析结束，开始保存数据到数据库");
						int repeatCount=onixDataService.saveOnixXmlFileReadOnixBookBaseInfoVo(infoVo);
						logger.info("文件："+fileInfoVo.getFileName()+"数据保存结束");
						synchronized (this) {
							if(infoVo.getSuccessfulParsed()){
								ftpFileInfoService.updateSuccessFulParsedAndSaveToDataBaseFtpFileInfo(fileInfoVo.getId(), fileInfoVo.getTotalRecordCount(),repeatCount);
								String newPath=fileInfoVo.getLocalFtpFilePath().replace(ftpConfigInfo.getLocalxmlFilePath(),ftpConfigInfo.getLocalmovedXmlFilePath()).replace(fileInfoVo.getFileName(), "");
								FileHelper.moveFileToNewPath(fileInfoVo.getLocalFtpFilePath(),newPath);
							}	
						}
					}else{
						synchronized (this) {
							ftpFileInfoService.updateUnSuccessFulParsed(fileInfoVo.getId(), infoVo.getParsingResult());
						}
					}
				}
				fileParseRunnableVo.adddealItem(fileInfoVo, true);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
				}
			} catch (Exception e) {
				if(fileInfoVo!=null){
					fileParseRunnableVo.adddealItem(fileInfoVo,false);
				    logger.error("文件："+fileInfoVo.getFileName()+"解析出错:"+e.getMessage().toString());
				}
			}
		}
	}
	
} 